package query;

import gui.Main;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import no.ntnu.fp.model.Room;

/**
 * Class for Accsessing the Table Rom(Rooms)
 * 
 * @author Tom & Magnus
 *
 */
public class RoomQuery {
	private static ResultSet rs;

	/**
	 * Method for getting available rooms at a current time
	 * 
	 * @param maxSize int
	 *  - The size of the party, rooms with a maxsize smaller then this will not be returned.
	 * @param start long
	 *  - Start of the Activity
	 * @param slutt long
	 *  - The end of the activity
	 * @return Arraylist<Room>
	 *  - with all the rooms available for the Activity
	 */
	public static ArrayList<Room> getRooms(int maxSize, long start, long slutt)  {
		ArrayList <Room> retur = new ArrayList <Room>();
		try {
			String hei = "SELECT * FROM ROM WHERE RomID NOT IN(SELECT MoteRomID " +
					"FROM KalenderPunkt WHERE (StartTidspunkt >=" + start + " AND StartTidspunkt <" +
					slutt + ") OR (SluttTidspunkt >" + start + " AND SluttTidspunkt <=" + slutt +
					") OR ( StartTidspunkt <=" + start + " AND SluttTidspunkt >=" + slutt +
					")) AND RomStorrelse>=" + maxSize/**/;
			System.out.println(hei);
			/*  There are three different reasons to not return a room.
			 *  either the room starts being used before the planned meeting, and isn't yet available
			 *  or the room will be taken over half way through the meeting
			 *  or the meeting will be interrupted as the room is reserved for something else in the 
			 *  middle
			 *  All three situations must be accounted for.
			 */ 

			
			Statement stmt = Main.con.createStatement();
			rs = stmt.executeQuery(hei);
			while (rs.next()){
				retur.add(new Room(rs.getString("RomID"), rs.getInt("RomStorrelse")));
			}
			System.out.println(retur);
			
		} catch (SQLException e) {
			
			e.printStackTrace();
			return null;
		}
		return retur;
	}
	/**
	 * A Method for Getting all the Possible Rooms
	 * 
	 * @return ArrayList <Room>
	 *  - All the Rooms in the Database
	 */
	public static ArrayList<Room> getAllRooms()  {
		ArrayList <Room> retur = new ArrayList <Room>();
		try {
			String hei = "SELECT * FROM ROM";

			Statement stmt = Main.con.createStatement();
			rs = stmt.executeQuery(hei);
			while (rs.next()){
				retur.add(new Room(rs.getString("RomID"), rs.getInt("RomStorrelse")));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return retur;
	}
}
